#include "test1.h"
#include "ui_test1.h"
#include <QMessageBox>

test1::test1(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::test1)
{
    ui->setupUi(this);
    model = new QStandardItemModel(0, 3, this);
    model->setHorizontalHeaderLabels({"学号", "姓名", "成绩"});
    ui->tableView->setModel(model);
    loadDataFromFile();
}

test1::~test1()
{
    saveDataToFile();
    delete ui;
}

void test1::loadDataFromFile()
{
    QFile file("students.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString line = in.readLine();
            QStringList fields = line.split(",");
            if (fields.size() == 3) {
                int row = model->rowCount();
                model->insertRow(row);
                model->setItem(row, 0, new QStandardItem(fields[0]));
                model->setItem(row, 1, new QStandardItem(fields[1]));
                model->setItem(row, 2, new QStandardItem(fields[2]));
            }
        }
        file.close();
    }
}

void test1::saveDataToFile()
{
    QFile file("students.txt");
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream out(&file);
        for (int i = 0; i < model->rowCount(); ++i) {
            QString id = model->item(i, 0)->text();
            QString name = model->item(i, 1)->text();
            QString score = model->item(i, 2)->text();
            out << id << "," << name << "," << score << "\n";
        }
        file.close();
    }
}

void test1::on_pushButton_2_clicked()
{
    QString id = ui->lineEdit->text();
    QString name = ui->lineEdit_2->text();
    QString score = ui->lineEdit_3->text();

    if (id.isEmpty() || name.isEmpty() || score.isEmpty()) {
        QMessageBox::warning(this, "警告", "请填写完整信息");
        return;
    }

    int row = model->rowCount();
    model->insertRow(row);
    model->setItem(row, 0, new QStandardItem(id));
    model->setItem(row, 1, new QStandardItem(name));
    model->setItem(row, 2, new QStandardItem(score));

    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_3->clear();
}

void test1::on_pushButton_5_clicked()
{
    // 简单实现，根据学号查询
    QString id = ui->lineEdit->text();
    if (id.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入学号");
        return;
    }

    for (int i = 0; i < model->rowCount(); ++i) {
        if (model->item(i, 0)->text() == id) {
            ui->lineEdit_2->setText(model->item(i, 1)->text());
            ui->lineEdit_3->setText(model->item(i, 2)->text());
            return;
        }
    }
    QMessageBox::information(this, "提示", "未找到该学生信息");
}

void test1::on_pushButton_3_clicked()
{
    QString id = ui->lineEdit->text();
    QString name = ui->lineEdit_2->text();
    QString score = ui->lineEdit_3->text();

    if (id.isEmpty() || name.isEmpty() || score.isEmpty()) {
        QMessageBox::warning(this, "警告", "请填写完整信息");
        return;
    }

    for (int i = 0; i < model->rowCount(); ++i) {
        if (model->item(i, 0)->text() == id) {
            model->setItem(i, 1, new QStandardItem(name));
            model->setItem(i, 2, new QStandardItem(score));
            QMessageBox::information(this, "提示", "修改成功");
            return;
        }
    }
    QMessageBox::information(this, "提示", "未找到该学生信息");
}

void test1::on_pushButton_4_clicked()
{
    QString id = ui->lineEdit->text();
    if (id.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入学号");
        return;
    }

    for (int i = 0; i < model->rowCount(); ++i) {
        if (model->item(i, 0)->text() == id) {
            model->removeRow(i);
            QMessageBox::information(this, "提示", "删除成功");
            return;
        }
    }
    QMessageBox::information(this, "提示", "未找到该学生信息");
}

void test1::on_pushButton_clicked()
{
    // 映射 QComboBox 索引到 model 列索引
    int column = ui->combox_value->currentIndex();
    if (column < 0 || column >= model->columnCount()) {
        QMessageBox::warning(this, "错误", "选择的列无效");
        return;
    }

    // 获取排序顺序
    Qt::SortOrder order = ui->comboBox_sort->currentIndex() == 0 ? Qt::AscendingOrder : Qt::DescendingOrder;

    // 执行排序
    model->sort(column, order);

    // 获取列名和排序顺序名称
    QString columnName = model->horizontalHeaderItem(column)->text();
    QString orderName = (order == Qt::AscendingOrder) ? "正序" : "倒序";

    // 显示提示信息
    QMessageBox::information(this, "提示", QString("已按%1列进行%2排序").arg(columnName, orderName));
}

void test1::on_pushButton_stats_clicked()
{
    int total = model->rowCount();
    double sum = 0;
    int validScores = 0;

    for (int i = 0; i < total; ++i) {
        bool ok;
        double score = model->item(i, 2)->text().toDouble(&ok);
        if (ok) {
            sum += score;
            ++validScores;
        }
    }

    double average = validScores > 0 ? sum / validScores : 0;
    QMessageBox::information(this, "统计结果", QString("总人数：%1\n有效成绩人数：%2\n平均成绩：%3")
                              .arg(total)
                              .arg(validScores)
                              .arg(average, 0, 'f', 2));
}